﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNetStar.TheKing.Infrastructure;
using Microsoft.Extensions.Caching.Memory;

namespace DotNetStar.TheKing.Services
{
    using Domain;
    using SDDP;
    using System.Linq;
    using System.Threading.Tasks;

    /// <summary>
    /// 积分服务
    /// </summary>
    public class PointService : ServiceBase
    {
        private IStorage<PointsStrategy,string> _pointStrategyStorage;

        public PointService(IStorage<PointsStrategy, string> pointStrategyStorage, IMemoryCache memoryCache) : base(memoryCache)
        {
            this._pointStrategyStorage = pointStrategyStorage;
        }

        /// <summary>
        /// 获取积分策略集合。
        /// </summary>
        /// <returns></returns>
        public Task<List<PointsStrategy>> GetPointsStrategiesAsync()
        =>
            GetFromCacheAsync("pointStrategies", () => _pointStrategyStorage.Query().ToList());

        /// <summary>
        /// 获取指定积分策略。
        /// </summary>
        /// <param name="strategyTypes">积分策略类型。</param>
        /// <returns></returns>
        public Task<PointsStrategy> GetPointStrategyAsync(PointStrategyTypes strategyTypes)
            => GetPointStrategyAsync(strategyTypes.ToString());

        /// <summary>
        /// 获取指定积分策略。
        /// </summary>
        /// <param name="strategyTypes">积分策略类型。</param>
        /// <returns></returns>
        public async Task<PointsStrategy> GetPointStrategyAsync(string pointStrategyId)
            => (await GetPointsStrategiesAsync()).SingleOrDefault(m => m.Id == pointStrategyId);
    }
}
